<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Job extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->check_authentication(2);
		$this->load->model('Job_model', 'job');
		$this->lang->load('company', $this->session->userdata['user_lang']);
		$this->lang->load('company/job', $this->session->userdata['user_lang']);
		$this->lang->load('company/job/subscriptions', $this->session->userdata['user_lang']);
	}
	
	public function index()
	{
		$job_list = $this->job->get(array('idcompany' => $this->session->userdata['idprofile']));
				
		$grid_job = array();
        
        foreach ($job_list->result() as $row)
		{
			$grid_job[] = '<div class="row"><span><span class="gtitle">' . $row->title .'</span>
				<div class="gtool">
				<a href="'.base_url().'company/job/subscriptions/'.$row->id.'">'.$this->lang->line('job_viewsubscriptions').'</a>
				&middot; <a onclick="CompanyAvailableJobs.editJob('.$row->id.');">'.$this->lang->line('job_showdetails').'</a>
				 &middot; <a onclick="CompanyAvailableJobs.deleteJob('.$row->id.');">'.$this->lang->line('global_delete').'</a>
				 </div>
			 </div>';
		}
		
		if (!$grid_job) {
			 $data['grid_job'] = '<center>'.$this->lang->line('job_noresults').'</center>';
		} else {
			 $data['grid_job'] = implode('<hr/>', $grid_job);
		}
		
		exitHTML($data['grid_job']);
	}
	
	public function insert()
	{
		$data = $_POST;
		
		if (missing_field($data, array('title', 'idcategory', 'description')))
		{
			formError($this->lang->line('job_missingfield'), true);
		}
		
		$data['idcompany'] = $this->session->userdata['idprofile'];
		checkbox2db(array('idtravel', 'idflit', 'idhomeoffice', 'iddeficiency', 'idcoursecompleted'), $data);
		
		if ($this->job->insert($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true);
		}
		else
		{
			exitJs('CompanyAvailableJobs.getAvailableJobsGrid();closeBox();');
		}
	}
	
	public function update($id_job = '')
	{
		$data = $_POST;
		
		if (missing_field($data, array('title', 'idcategory', 'description')))
		{
			formError($this->lang->line('job_missingfield'), true);
		}
		
		checkbox2db(array('idtravel', 'idflit', 'idhomeoffice', 'iddeficiency', 'idcoursecompleted'), $data);
		
		if ($this->job->update($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true);
		}
		else
		{
			exitJs('CompanyAvailableJobs.getAvailableJobsGrid();closeBox();');
		}
	}
	
	public function delete()
	{
		$data = $_POST;
		
		if (missing_field($data, array('id')))
		{
			formError($this->lang->line('job_missingfield'));
		}
		
		if ($this->job->delete(array('id' => $data['id'])) === false)
		{
		   formError($this->lang->line('global_systemfail'));
		}
		else
		{
			exitJs('CompanyAvailableJobs.getAvailableJobsGrid();');
		}
	}
	
	public function edit($id_job = '')
	{
		header("Content-type: text/html; charset=utf-8");
		$data = $_POST;
		$data['id_job'] = $id_job;
		$data['title'] = '';
		$data['description'] = '';
		$data['keywords'] = '';
		$data['minsalary'] = '';
		$data['maxsalary'] = '';
		
		if (is_numeric($id_job))
		{
			$job_data = $this->job->get(array('idjob' => $id_job))->row();
			$data['title'] = $job_data->title;
			$data['description'] = $job_data->description;
			$data['minsalary'] = $job_data->minsalary;
			$data['maxsalary'] = $job_data->maxsalary;
			
			$keywords = array();
			$keywords_list = $this->job->get_job_keywords(array('idjob' => $id_job));
			foreach ($keywords_list->result() as $line)
			{
				$keywords[] = $line->name;
			}
			$data['keywords'] = implode(', ', $keywords);
			
			$data['set_category_value'] = '$("#idcategory").val("'.$job_data->idcategory.'");';
			$data['extrascript'] = '';
			$data['extrascript'] .= '$("#idhierarchy").val('.$job_data->idhierarchy.');';
			$data['extrascript'] .= '$("#idspecialty").val('.$job_data->idspecialty.');';
			$data['set_country_value'] = '$("#idcountry").val("'.$job_data->idcountry.'");';
			$data['set_level_value'] = '$("#idlevel").val("'.$job_data->idcourselevel.'");';
			$data['extrascript'] .= '$("#idcoursetype").val('.$job_data->idcoursetype.');';
			$data['extrascript'] .= '$("#idstate").val('.$job_data->idstate.');';
			$data['extrascript'] .= '$("#iddrivelicense").val('.$job_data->iddrivelicense.');';
			
			if ($job_data->idtravel == 'Y')
	        {
				$data['extrascript'] .= '$("#idtravel").attr("checked", true);';
			}
			
			if ($job_data->idflit == 'Y')
			{
				$data['extrascript'] .= '$("#idflit").attr("checked", true);';
			}
			
			if ($job_data->idhomeoffice == 'Y')
			{
				$data['extrascript'] .= '$("#idhomeoffice").attr("checked", true);';
			}
			
			if ($job_data->iddeficiency == 'Y')
			{
				$data['extrascript'] .= '$("#iddeficiency").attr("checked", true);';
			}
			
			if ($job_data->idcoursecompleted == 'Y')
			{
				$data['extrascript'] .= '$("#idcoursecompleted").attr("checked", true);';
			}
		}
		
		$data['userdata'] = $this->session->userdata;
        $this->load->view('company/job', $data);
	}
	
	public function add()
	{
		$this->edit();
	}
	
	public function subscriptions($job_id = null)
	{
		$this->session->set_userdata(array('idjob' => $job_id));
		
		$job_data = $this->job->get(array('idjob' => $job_id))->row();
		
		$data['main_content'] = 'company/job/subscriptions';
        $data['userdata'] = $this->session->userdata;
        $data['page_title'] = sprintf($this->lang->line('subscriptions_title'), $job_data->title);
        $this->load->view('template-company', $data);
	}
	
	public function get_job_subscriptions($category = 'available')
	{
		$this->load->model('Subscription_model', 'subscription');
		
		switch ($category)
		{
			case 'discarded':
				$array_status = array(5);
				break;
				
			case 'engaged':
				$array_status = array(6);
				break;
				
			default:
				$array_status = array(1,2,3,4);
				break;
		}
		
		$job_list = $this->subscription->get(array('idjob' => $this->session->userdata['idjob'], 'idstatus' => $array_status));
		
		$grid_job = array();
        
        foreach ($job_list->result() as $row)
		{
			switch ($row->idstatus)
			{
				case 2:
					$status = $this->lang->line('subscriptions_seen');
					break;
				case 3:
					$status = $this->lang->line('subscriptions_evaluation');
					break;
				case 4:
					$status = $this->lang->line('subscriptions_finalist');
					break;
				case 5:
					$status = $this->lang->line('subscriptions_discarded');
					break;
				case 1:
				default:
					$status = $this->lang->line('subscriptions_unseen');
					break;
			}
			
			$candidate_profile_link = '<a href="'.base_url().'profile/candidate/'.$row->idcandidate.'/'.strtolower($row->firstname) . '-' . strtolower($row->lastname) . '/'.$row->idsubscription.'">';
			$candidate_image = $row->thumbnail ? ($row->idmember.'/tmb/'.$row->thumbnail) : 'src/anonymous_thumb.png';
			
			$grid_job[] = '<div class="row">
				<span>
				'.$candidate_profile_link.'<img src="'.base_url().'f/'.$candidate_image.'" class="vmiddle" /></a>
				<span class="gtitle">' . $candidate_profile_link . $row->firstname . ' ' . $row->lastname .'</a>'.($category == 'available' ? ' ('.$status.')' : '').'</span>
				<div class="gtool">'
					. $candidate_profile_link
					. $this->lang->line('subscriptions_showcandidateprofile').'</a>
				 </div>
			 </div>';
		}
		
		if (!$grid_job) {
			 $data['grid_job'] = '<center>'.$this->lang->line('subscriptions_'.$category.'noresults').'</center>';
		} else {
			 $data['grid_job'] = implode('<hr/>', $grid_job);
		}
		
		exitHTML($data['grid_job']);
	}
}

?>